package com.example.sxt_practise.IO;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

/**
 * FileInputStream
 * @author gaofeng
 * @date 2022年05月08日 16:20
 */
public class IoTest02 {
    public static void main(String[] args) {
        // 1.创建源
        File dir = new File("dir/test2/index.html");
        // 2.选择流
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(dir);
            // 3. 操作,分段读取
            byte[] flush = new byte[1024 * 2];
            int len = -1;
            while ((len = inputStream.read(flush)) != -1) {
                String str = new String(flush, 0, len);
                System.out.println(str);
            }
            // 关闭资源
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (Objects.nonNull(inputStream)) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
